//
//  loginTableViewCell.m
//  jinling
//
//  Created by mac on 2018/9/30.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "loginTableViewCell.h"

@implementation loginTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)cratView{
    UILabel *label=[UILabel new];
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    label.text=@"+86";
    label.font=[UIFont systemFontOfSize:14];
    
    
    UILabel *label2=[UILabel new];
    [self addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label.mas_right).mas_equalTo(30);
        make.centerY.mas_equalTo(label);
    }];
    label2.text=@"中国";
    label2.font=[UIFont systemFontOfSize:14];
    
    
    UIView *xianView=[UIView new];
    [self addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianView.backgroundColor=_LightGrey;
    
    UILabel *label3=[UILabel new];
    [self addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(xianView.mas_bottom).mas_equalTo(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    label3.text=@"手机号码";
    label3.font=[UIFont systemFontOfSize:14];
    
    _phoneText=[UITextField new];
    [self addSubview:_phoneText];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label2);
        make.centerY.mas_equalTo(label3);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(20);
    }];
    _phoneText.placeholder=@"请输入手机号码";
    _phoneText.font=[UIFont systemFontOfSize:14];
    _phoneText.returnKeyType=UIReturnKeyDone;
    _phoneText.delegate=self;
    
    UIView *xianView2=[UIView new];
    [self addSubview:xianView2];
    [xianView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label3.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianView2.backgroundColor=_LightGrey;
    
    
    _label4=[UILabel new];
    [self addSubview:_label4];
    [_label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(xianView2.mas_bottom).mas_equalTo(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    _label4.text=@"登录密码";
    _label4.font=[UIFont systemFontOfSize:14];
    
    _pwdText=[UITextField new];
    [self addSubview:_pwdText];
    [_pwdText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label2);
        make.centerY.mas_equalTo(self->_label4);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(20);
    }];
    _pwdText.placeholder=@"请输入登陆密码";
    _pwdText.font=[UIFont systemFontOfSize:14];
    _pwdText.returnKeyType=UIReturnKeyDone;
    _pwdText.delegate=self;
    _pwdText.secureTextEntry=YES;//密文

    
    UIView *xianView3=[UIView new];
    [self addSubview:xianView3];
    [xianView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(self->_label4.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(1);
    }];
    xianView3.backgroundColor=_LightGrey;
    
    
    UIButton *btn=[UIButton new];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(xianView3.mas_bottom).mas_equalTo(25);
        make.height.mas_equalTo(40);
    }];
    btn.layer.cornerRadius=5;
    btn.backgroundColor=_backgroundColor;
    [btn setTitle:@"登录" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:17];
    [btn addTarget:self action:@selector(denglu) forControlEvents:UIControlEventTouchUpInside];
    
//    UIView *xianview4=[UIView new];
//    [self addSubview:xianview4];
//    [xianview4 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(0);
//        make.top.mas_equalTo(btn.mas_bottom).mas_equalTo(40);
//        make.width.mas_equalTo(1);
//        make.height.mas_equalTo(15);
//    }];
//    xianview4.backgroundColor=[UIColor blackColor];
//
    _label5=[UIButton new];
    [self addSubview:_label5];
    [_label5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(btn.mas_bottom).mas_equalTo(40);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    [_label5  setTitle:@"手机登录" forState:0];
    [_label5 setTitleColor:[UIColor blackColor] forState:0];
    _label5.titleLabel.font=[UIFont systemFontOfSize:14];
    [_label5 addTarget:self action:@selector(shouJidenglu) forControlEvents:UIControlEventTouchUpInside];
    _label5.titleLabel.textAlignment=NSTextAlignmentRight;
    
    
//    UILabel *label6=[UILabel new];
//    [self addSubview:label6];
//    [label6 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(xianview4.mas_right).mas_equalTo(5);
//        make.centerY.mas_equalTo(xianview4);
//    }];
//    label6.text=@"忘记密码";
//    label6.font=[UIFont systemFontOfSize:14];

}
-(void)shouJidenglu{


    if ([_label5.titleLabel.text isEqualToString:@"手机登录"]) {
        _label4.text=@"短信验证码";
        [_label5 setTitle:@"密码登录" forState:0];
        [_pwdText removeFromSuperview];
        
        _codeText=[UITextField new];
        [self addSubview:_codeText];
        [_codeText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self->_label4.mas_right).mas_equalTo(30);
            make.centerY.mas_equalTo(self->_label4);
            make.right.mas_equalTo(-100);
            make.height.mas_equalTo(20);
        }];
        _codeText.delegate=self;
        _codeText.placeholder=@"请输入验证码";
        _codeText.font=[UIFont systemFontOfSize:14];
        _codeText.returnKeyType=UIReturnKeyDone;
//        _codeText.keyboardType=UIKeyboardTypePhonePad;
        
        
        _codeBtn=[UIButton new];
        [self addSubview:_codeBtn];
        [_codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(80);
            make.centerY.mas_equalTo(self->_codeText);
        }];
        _codeBtn.layer.masksToBounds=YES;
        _codeBtn.layer.cornerRadius=5;
        _codeBtn.layer.borderWidth=1;
        _codeBtn.layer.borderColor=_backgroundColor.CGColor;
        [_codeBtn setTitle:@"发送验证码" forState:0];
        [_codeBtn setTitleColor:_backgroundColor forState:0];
        _codeBtn.titleLabel.font=[UIFont systemFontOfSize:13];
        [_codeBtn  addTarget:self action:@selector(huoQuYanZhen:) forControlEvents:UIControlEventTouchUpInside];
        return;
        
    }
    if ([_label5.titleLabel.text isEqualToString:@"密码登录"]) {
        _label4.text=@"登录密码";
        [_label5 setTitle:@"手机登录" forState:0];
        
        [_codeText removeFromSuperview];
        [_codeBtn removeFromSuperview];
        
        _pwdText=[UITextField new];
        [self addSubview:_pwdText];
        [_pwdText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self->_label4.mas_right).mas_equalTo(30);
            make.centerY.mas_equalTo(self->_label4);
            make.right.mas_equalTo(-30);
            make.height.mas_equalTo(20);
        }];
        _pwdText.placeholder=@"请输入手机号码";
        _pwdText.font=[UIFont systemFontOfSize:14];
        _pwdText.returnKeyType=UIReturnKeyDone;
        _pwdText.delegate=self;
        _pwdText.secureTextEntry=YES;//密文
        return;
    }
    
}
-(IBAction)huoQuYanZhen:(UIButton *)sender{
    
    
    
    NSString *mobile=_phoneText.text;
    if (![gongju isMobileNumber:mobile]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    NSString *url=[NSString stringWithFormat:@"user/user_byphone?phone=%@",mobile];
    [gongju AFNGETDataUrl:url andDic:nil andTableView:nil andView:self andSuccessBlock:^(id responseObject) {

        BaseModel *dataModel;

        NSDictionary *typedic=[responseObject objectForKey:@"error"];
        if (typedic==nil) {
            dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
        }else{
            dataModel = [BaseModel mj_objectWithKeyValues:[responseObject objectForKey:@"error"]];
        }

        if (dataModel.code==100) {
            [self faSongYanZhen];
        }
        else{
            [gongju svpInfo:dataModel.msg];
        }
    } anderrorBlock:nil];
    


}

-(void)faSongYanZhen{
    NSString *mobile=_phoneText.text;
    if (![gongju isMobileNumber:mobile]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:mobile,@"phone",@"1",@"code", nil];
    [gongju AFNPOSTDataUrl:@"sms/send_sms" andDic:dic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
    
        self->_codeBtn.enabled=NO;
        self->_timeTick=60;
        self->_timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        
        NSLog(@"%@",responseObject);
    } anderrorBlock:nil];
}

-(void)timeFireMethod
{
    if (_timeTick==0) {
        _codeBtn.enabled=YES;
        _codeBtn.layer.cornerRadius=5;
        _codeBtn.layer.borderWidth=1;
        _codeBtn.layer.borderColor=_backgroundColor.CGColor;
        _codeBtn.layer.borderColor=_backgroundColor.CGColor;
        [_codeBtn setTitle:@"发送验证码" forState:0];
        [_codeBtn setTitleColor:_backgroundColor forState:0];
        _codeBtn.titleLabel.font=[UIFont systemFontOfSize:13];
        _codeBtn.backgroundColor=[UIColor clearColor];
        [_timer invalidate];
        return;
    }
    _timeTick--;
    [_codeBtn setTitle:[NSString stringWithFormat:@"%dS重发",_timeTick] forState:0];
    _codeBtn.backgroundColor=_DarkGrey;
    _codeBtn.layer.cornerRadius=5;
    _codeBtn.layer.borderWidth=0;
    _codeBtn.layer.borderColor=[UIColor clearColor].CGColor;
    _codeBtn.layer.borderColor=[UIColor clearColor].CGColor;
    [_codeBtn setTitleColor:_backgroundColor forState:0];
    _codeBtn.titleLabel.font=[UIFont systemFontOfSize:13];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)denglu{
    
    NSString *phoneStr=_phoneText.text;
    if (![gongju isMobileNumber:phoneStr]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    if ([_label5.titleLabel.text isEqualToString:@"手机登录"]) {
        NSString *pwdStr=_pwdText.text;
        if ([gongju charIsNil:pwdStr]||pwdStr.length<6||pwdStr.length>16) {
            [gongju svpInfo:@"密码格式不对"];
            return;
        }
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:phoneStr,@"phone",pwdStr,@"password", nil];
        [gongju AFNPOSTDataUrl:@"user/appLoging" andDic:dic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
            
            [gongju svpInfo:@"登陆成功"];
            if ( [NSKeyedArchiver archiveRootObject:responseObject toFile:_userModelFile]) {
                [self.viewController.navigationController popViewControllerAnimated:YES];
            }
        } anderrorBlock:nil];
        
    }
    else{
        NSString *pwdStr=_codeText.text;
        if ([gongju charIsNil:pwdStr]) {
            [gongju svpInfo:@"验证码不能为空"];
            return;
        }
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:phoneStr,@"phone",pwdStr,@"code", nil];
        [gongju AFNPOSTDataUrl:@"user/code_login" andDic:dic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
            [gongju svpInfo:@"登陆成功"];
            if ( [NSKeyedArchiver archiveRootObject:responseObject toFile:_userModelFile]) {
                [self.viewController.navigationController popViewControllerAnimated:YES];
            }
        } anderrorBlock:nil];
        
    }
}
@end
